﻿using MediatR;
using Microsoft.IdentityModel.Tokens;
using Mysqlx.Crud;
using Production_Get.API.Application.Commands.OrderShow;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.BasicInformation.warehouses;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Get.API.Application.Commands.Warehousingtes
{
    //入库列表
    public class StorageSrcetenHandler : IRequestHandler<StorageSrcetenCommend, Result>
    {
        private readonly IBaseRepositoryGet<StorageDto> StorageDto;

        public StorageSrcetenHandler(IBaseRepositoryGet<StorageDto> StorageDto)
        {
            this.StorageDto = StorageDto;
        }

        public async Task<Result> Handle(StorageSrcetenCommend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Search };

            string sql = "SELECT * FROM Storage a INNER JOIN PurchasingType b on a.PurchasingTypeId=b.PurchasingTypeId INNER JOIN Purchasings c on a.PurchasinId=c.PurchasinId" +
              " INNER JOIN Warehouse d on a.WarehouseId = d.WarehouseId where a.IsDelete = 0";
            StorageDto come = new StorageDto();
            var list = StorageDto.GetAll(sql, come).ToList();

            if (!string.IsNullOrEmpty(request.StorageNo))
            {
                list = list.Where(x => x.StorageNo.Contains(request.StorageNo)).ToList();
            }

            if (!string.IsNullOrEmpty(request.StorageType))
            {
                list = list.Where(x => x.StorageType.Contains(request.StorageType)).ToList();
            }

            if (request.PurchasingTypeId > 0)
            {
                list = list.Where(x => x.PurchasingTypeId == request.PurchasingTypeId).ToList();
            }

            if (request.PurchasinId > 0)
            {
                list = list.Where(x => x.PurchasinId == request.PurchasinId).ToList();
            }

            if (!string.IsNullOrEmpty(request.StorageDate))
            {
                list = list.Where(x => x.StorageDate ==Convert.ToDateTime( request.StorageDate)).ToList();
            }

            if (request.StorageState != null)
            {
                list = list.Where(x => x.StorageState == request.StorageState).ToList();
            }

            var totalcount = list.Count();
            var pagecount = (int)Math.Ceiling(totalcount * 1.0 / request.pageSize);
            list = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();

            var lief = new
            {
                totalcount,
                pagecount,
                list,
            };

            if (list != null)
            {
                result.Code = (int)CommonErrorCodes.Success;
                result.Data = lief;
                result.Msg = "显示成功";
            }
            else
            {
                result.Msg = "显示失败";
            }
            return result;
        }
    }
}
